import { axios } from '@/utils/request'
// import { Tip } from '@/utils'
import qs from 'qs'
import router from '@/router'
function parse(data) {
  const result = {}
  for (const key in data) {
    const v = data[key]
    if (typeof v === 'string') {
      result[key] = v
    } else {
      result[key] = JSON.stringify(v)
    }
  }
  return qs.stringify(result)
}

function wapper(method, url, data = {}, config) {
  const c = Object.assign({ errorTip: true }, config)
  // console.log(c, 'c')
  const { json = true } = config || { json: true }
  let headers = c.headers || { 'Content-Type': 'application/json;charset=UTF-8' }
  if (c.file) {
    headers = { 'Content-Type': 'multipart/form-data' }
  }
  if (window.vueInstance) {
    data.menu_mark = window.vueInstance.$route.path
  }
  //
  const params = {
    url,
    method,
    headers
  }
  if (['delete', 'get'].includes(method)) {
    params.params = data
  } else {
    params.data = data
  }
  if (['post', 'put'].includes(method)) {
    if (!json) {
      params.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    }
    params.data = json ? data : parse(data)
  } else {
    params.params = data
  }
  // if(method === 'postFrom'){
  //     params.data = c.file ? data : qs.stringify(data)
  // }

  return axios(params)
    .then(res => {
      console.log(res,'baocun')
      if (c.process) {
        // console.log('baocun')
        const { success, result, message } = c.process(res)

        if (success) {
          return Promise.resolve(result ? result : res)
        } else {
          return Promise.reject(message)
        }
      }
      const { code, success, result, data,message } = res
      if (success || [0, 200].includes(code)) {
        return c.full ? res : data || result ? result : res;
      } else {
        return Promise.reject(message)
      }
    })
    .catch(e => {
      const error = e && e.message ? e.message : e
      if (c.errorTip && error) {
       
        
        //   Tip.error(error)
          setTimeout(() => {
            if("Token失效，请重新登录"==error){
              location.reload()
            }
          }, 2000);
          
           //不行
          // router.push({
          //   path: '/user/login',
          // });
      }
      return Promise.reject(error)
    })
}

export function post(url, data, config) {
  return wapper('post', url, data, { json: true, ...config })
}
export function postFile(url, data, config) {
  return wapper('post', url, data, { file: true, ...config })
}
export function postFrom(url, data, config) {
  return wapper('post', url, data, { json: false, ...config })
}
export function put(url, data, config) {
  return wapper('put', url, data, config)
}
export function putFrom(url, data, config) {
  return wapper('put', url, data, { json: false, ...config })
}
export function _delete(url, data, config) {
  return wapper('delete', url, data, config)
}
export function get(url, data, config) {
  return wapper('get', url, data, config)
}
